﻿using B.S.DataBase.SuY.Read.Api.AutoList.Command;
using B.S.DataBase.SuYuan.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace B.S.DataBase.SuY.Read.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class GoodsController : ControllerBase
    {

        private readonly IMediator mediator;

        /// <summary>
        /// 你们，
        /// </summary>
        /// <param name="mediator"></param>
        public GoodsController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 添加商品
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>

        [HttpPost]
        public Task<ApiReslit<object>> AddGoods(AddGoodsCommand request)
        {

            return mediator.Send(request);
        }

        /// <summary>
        /// 次v帮你们，
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>

        [HttpPost]
        public async Task<IActionResult> UploadImages(IFormFile file)
        {

            try
            {
                var res= "wwwroot/Proimg/"+ file.FileName;
                using (FileStream stream = new FileStream(res, FileMode.OpenOrCreate))
                {
                    file.CopyTo(stream);
                }

                return Ok("https://localhost:7045/Proimg/" + file.FileName);
            }
            catch (Exception)
            {

                throw;
            }

            }

        [HttpDelete]
        public Task<ApiReslit<object>> DelGood(int G_Id)
        {
            DelGoodesCommand command = new DelGoodesCommand();
            command.G_Id = G_Id;
            return mediator.Send(command);

        }

        [HttpPut]
        public Task<ApiReslit<object>> UpdateGood(UpdateGoodsCommand request)
        {
            return mediator.Send(request);
        }


        [HttpDelete]
        public Task<ApiReslit<object>> DelListGood([FromQuery]DelListGoodsCommand request)
        {
            return mediator.Send(request);

        }
     }
}
